package com.lanou3g.HW_0517.view;

import com.lanou3g.HW_0517.controller.Controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by dllo on 18/5/16.
 */
@WebServlet(name = "RegisterServlet", urlPatterns = "/register")
public class RegisterServlet extends HttpServlet {
    private Controller controller = new Controller();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //TODO 拿到用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String repwd = request.getParameter("repwd");
        String nickname = request.getParameter("nickname");
        String hobby = request.getParameter("hobby");
        String gender = request.getParameter("gender");

        //TODO 验证

        boolean registerSuccess = controller.register(username, password,repwd,nickname,hobby,gender);

        //TODO 根据验证结果构建验证信息
        String msg = "";
        if (registerSuccess) {
            response.sendRedirect("/index.jsp");
        } else {

            // 把消息放到jsp域中 发送给jsp
            request.setAttribute("msg", "注册");
            request.setAttribute("url","/register.jsp");
            // 请求转发
            request.getRequestDispatcher("/msg.jsp").forward(request, response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);
    }
}
